<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>过滤器</title>

    <script src="../js/vue.js">

    </script>
</head>
<body>

<div id="root">
    <h3>现在的时间是： {{time | formatTime('yyyy-MM-dd') | slice(0,3)}}</h3>
</div>

<script type="text/javascript">
    Vue.config.printWarnings = false;
    //设置全局过滤器Vue.filter()方法
    // Vue.filter('formatTime', function (value, format = 'yyyy-MM-dd hh:mm:ss') {
    //     //将value转换为时间格式   2022-12-12 12:12:12
    //     let date = new Date(value);
    //     let year = date.getFullYear();
    //     let month = date.getMonth() + 1;
    //     let day = date.getDate();
    //     let hour = date.getHours();
    //     let minute = date.getMinutes();
    //     let second = date.getSeconds();
    //     if (format === 'yyyy-MM-dd') {
    //         return year + '-' + month + '-' + day;
    //     }
    //     return year + '-' + month + '-' + day +'' + hour + ':' + minute + ':' + second;
    // })
    const vm = new Vue({
        el: '#root',
        data: {
            time: 1734861111948
        },
        filters: {
            //value是要过滤的值 format是过滤器的参数
            //过滤器的返回值是过滤后的值
            formatTime: function (value, format = 'yyyy-MM-dd hh:mm:ss') {
                //将value转换为时间格式
                let date = new Date(value);
                let year = date.getFullYear();
                let month = date.getMonth() + 1;
                let day = date.getDate();
                let hour = date.getHours();
                let minute = date.getMinutes();
                let second = date.getSeconds();
                if (format === 'yyyy-MM-dd') {
                    return year + '-' + month + '-' + day;
                }
                return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
            },
            slice: function (value, start, end) {
                return value.slice(start, end);
            }
        }
    })

</script>
</body>

</html>